package com.netty.pro.netty.inbound_outbound;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author xuzn
 * @ClassName: MyByteToLongDecoder
 * @ProjectName netty-pro
 * @date 2020/3/20/8:03
 */
public class MyByteToLongDecoder extends ByteToMessageDecoder {
  private AtomicInteger i = new AtomicInteger(0);

  /**
   * @param ctx-上下文对象
   * @param in-入站的bytebuf
   * @param out-将解码后的数据传给下一个handler(InboundHandler)
   * @throws Exception
   */
  @Override
  protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    // java中long类型为8个字节
    if (in.readableBytes() >= 8) {
      out.add(in.readLong());
//      i.getAndIncrement();
//      System.out.println("decode 第"+  i + "次被调用");
      System.out.println("MyByteToLongDecoder 的decode方法被调用");
    }
  }
}
